from urllib import urlretrieve
import tarfile
import os
import re

def ext_match(fn, ext):
    return re.search(ext + "$", fn)

def get_externals():
    urls = ( ("http://www.qtrac.eu/pyqtbook.tar.gz", "pyqtbook.tar.gz" ) ,)
    local_dir = "local"
    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    for (remote,local) in urls:
        local = os.path.join(local_dir,local)
        if not os.path.exists(local):
            urlretrieve(remote,local)
        
        if ext_match(local, "tar.gz"):
            assert(tarfile.is_tarfile(local), "Error %s, retrieved from %s is not a tar file" % (local,remote))
            tar = tarfile.open(local, 'r:gz')
            tar.extractall(local_dir)
            tar.close()
    
if __name__=="__main__":
    get_externals()
